<?php


namespace app\admin\controller\mall;


use app\admin\model\MallTradeRecordLog;
use app\admin\model\MallWithdrawRecord;
use app\common\controller\AdminController;
use jianyan\excel\Excel;
use think\App;

class RewardUser extends AdminController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        
        $this->model = new \app\admin\model\MallUser();
        
        $this->assign('getStatusList', $this->model->getStatusList());
        
    }
    /**
     * @NodeAnotation(title="奖励记录")
     */
    public function reward()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            [$page, $limit, $where] = $this->buildTableParames();
            $where[] = ['ea_mall_trade_record_log.user_id', '=', input('id')];
            $where[] = ['trade_type', '=', 3];
            $count   = MallTradeRecordLog::where($where)
                ->count();
            $list    = MallTradeRecordLog::where($where)
                ->withJoin('mallOrder', 'LEFT')
                ->page($page, $limit)
                ->order($this->sort)
                ->select()
                ->toArray();
            $data    = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        $this->assign('id', input('id'));
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="提现记录导出")
     */
    public function export()
    {
        
        [$page, $limit, $where] = $this->buildTableParames();
        $header  = [
            ['订单号', 'order_no'],
            ['奖励金额', 'amount'],
            ['创建时间', 'create_time'],
        ];
        $where[] = ['ea_mall_trade_record_log.user_id', '=', input('id')];
        $where[] = ['trade_type', '=', 3];
        $list    = MallTradeRecordLog::where($where)
            ->withJoin('mallOrder', 'LEFT')
            ->limit(100000)
            ->order($this->sort)
            ->select()
            ->toArray();
        foreach ($list as $key => $value) {
            $list[ $key ][ 'amount' ]  = '￥' . $value[ 'amount' ] / 100 ?? 0;
            $list[ $key ][ 'order_no' ] = $value['mallOrder']['order_no'];
        }
        $fileName = '奖励记录导出' . time();
        return Excel::exportData($list, $header, $fileName, 'xlsx');
    }
}